<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'Savant2/Plugin.php';

class Savant2_Plugin_lgmngr_show_nextvs extends Savant2_Plugin{

	function plugin($project, $options){
		global $database;
		
		$favteam = $project->fav_team;
		$current_round = $project->current_round;

        $project->getRounds();
		
		if(count($rounds) > 0){
		
			$matchtable = LM::getDBTable("LMMatch");
			$matchtable->filterbyProject($project_id);
			$matchtable->filterbyRound($round_id);
			
			$matchtable->addFilter("(m.match_team1 = " . $database->Quote(intval($favteam)) . " OR m.match_team2 = " . $database->Quote(intval($favteam)) . ")");
			$matchtable->addFilter("m.published = 1");
			
			$rows = $matchtable->getRows();
        
            $team1 = LM::getDBTable("LMTeam");
			$team1->load($rows[0]->team1_id);
			
            $team2 = LM::getDBTable("LMTeam");
			$team2->load($rows[0]->team2_id);
			
			$team_id1 = $database->Quote(intval($team1->team_id));
			$team_id2 = $database->Quote(intval($team2->team_id));
			
			$matchtable2 = LM::getDBTable("LMMatch");
			$matchtable2->addFilter("m.published = 1");
			$matchtable2->addFilter("((m.match_team1 = $team_id1 AND m.matchteam2 = $team_id2) OR (m.match_team1 = $team_id2 AND m.match_team2 = $team_id1))");
			
            $matches = $matchtable2->getRows();
        
            $s = 0;
            $u = 0;
            $n = 0;
        
            if(count($matches) > 0){
                foreach($matches as $match){
                    if($match->match_id <> $rows[0]->match_id){
                        if($match->matchpart1 == $favteam){
                            if($match->matchpart1_result > $match->matchpart2_result) $s++;
                            if($match->matchpart1_result == $match->matchpart2_result) $u++;
                            if($match->matchpart1_result < $match->matchpart2_result) $n++;
                        }
                        if($match->matchpart2 == $favteam){
                            if($match->matchpart2_result > $match->matchpart1_result) $s++;
                            if($match->matchpart2_result == $match->matchpart1_result) $u++;
                            if($match->matchpart2_result < $match->matchpart1_result) $n++;
                        }
                    }  
                }
            }
        
            $matchpart1_pic = $teams[$teamid1]->logo;
            $matchpart2_pic = $teams[$teamid2]->logo;
  
            $matchpart1_pic = (trim($matchpart1_pic) != "")? sprintf('<img src="%s" width="54"/>', $matchpart1_pic) : "&nbsp;";
            $matchpart2_pic = (trim($matchpart2_pic) != "")? sprintf('<img src="%s" width="54"/>', $matchpart2_pic) : "&nbsp;";
			
			ob_start();

?>
            <table width="100%">
                <tr>
                    <td colspan="3" align="center">
                        <?php echo mosFormatDate($rows[0]->round_date, $options["dateformat"]);?><br/>
                    </td>
                </tr>
                <tr>
                    <td colspan="3" align="center"><?php echo $teams[$teamid1]->name;?></td>
                </tr>
            <?php   if($options["showpics"]){?>
                    <tr>
                        <td width="49%" align="center" valign="middle"><?php echo $matchpart1_pic;?></td>
                        <td width="2%"><b>vs.</b></td>
                        <td width="49%" align="center" valign="middle"><?php echo $matchpart2_pic;?></td>
                    </tr>
            <?php   }
                    else{ ?>
                    <tr>
                        <td colspan="3" align="center"><b>vs</b></td>
                    </tr>   
            <?php
                    } 
            ?>
                <tr>
                    <td colspan="3" align="center"><?php echo $teams[$teamid2]->name;?></td>
                </tr>
                <tr>
                    <td colspan="3" align="center">
                        <b>Spiele gegeneinander:</b><br/>
                        <?php printf("%s / %s / %s ( S / U / N )", $s, $u, $n);?><br/>
                    </td>
                </tr>
            </table>
<?php 
        }
    
        $html = ob_get_contents();
        ob_end_clean();
    
        return $html;
    }
}
?>